/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Partida.java
 *
 * Created on 04/06/2011, 10:50:47
 */

package campeonatobrasuka;

import Persistencia.SeqHelper;
import Persistencia.daos.CampeonatoDAO;
import Persistencia.daos.EstadioDAO;
import Persistencia.daos.PartidaDAO;
import Persistencia.daos.TimeDAO;
import Persistencia.modelos.Time;
import Persistencia.modelos.Estadio;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Jaqueline Ishiyama
 */
public class Partida extends javax.swing.JPanel {
DefaultFormatter form = new DefaultFormatter();
Collection<Estadio> estadios = new ArrayList<Estadio>();
Collection<Time> times = new ArrayList<Time>();
EstadioDAO estadioDao = new EstadioDAO();
TimeDAO timeDao = new TimeDAO();
    /** Creates new form Partida */
    public Partida() {
        initComponents();

         times = timeDao.recuperaTodos();

         jComboBox4.addItem("Selecione uma opção");
         jComboBox6.addItem("Selecione uma opção");

         for (Time time: times){
           jComboBox4.addItem(time.toString());
           jComboBox6.addItem(time.toString());
        }

         estadios = estadioDao.recuperaTodos();

         jComboBox1.addItem("Selecione uma opção");

        for (Estadio estadio: estadios){
           jComboBox1.addItem(estadio.toString());
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel6 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jComboBox6 = new javax.swing.JComboBox();
        jComboBox4 = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(campeonatobrasuka.CampeonatoBrasukaApp.class).getContext().getResourceMap(Partida.class);
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        jTextField4.setName("jTextField4"); // NOI18N

        jTextField3.setName("jTextField3"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setEnabled(false);
        jButton3.setName("jButton3"); // NOI18N

        jComboBox1.setName("jComboBox1"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jComboBox6.setName("jComboBox6"); // NOI18N
        jComboBox6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox6ActionPerformed(evt);
            }
        });

        jComboBox4.setName("jComboBox4"); // NOI18N

        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        jFormattedTextField1.setFormatterFactory(form.setFormatoData());
        jFormattedTextField1.setName("jFormattedTextField1"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText(resourceMap.getString("jRadioButton1.text")); // NOI18N
        jRadioButton1.setName("jRadioButton1"); // NOI18N

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText(resourceMap.getString("jRadioButton2.text")); // NOI18N
        jRadioButton2.setName("jRadioButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel11)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBox4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jRadioButton1)
                                    .addGap(18, 18, 18)
                                    .addComponent(jRadioButton2))
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(38, 38, 38)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox6, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(99, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(344, 344, 344)
                            .addComponent(jButton3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1))
                        .addComponent(jLabel12))
                    .addContainerGap(303, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jComboBox6))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2))
                .addGap(36, 36, 36))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(11, 11, 11)
                    .addComponent(jLabel12)
                    .addGap(240, 240, 240)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton3)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(12, 12, 12)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:
        // BOTÃO INSERIR
       Persistencia.modelos.Time timeV = new Persistencia.modelos.Time();
       Persistencia.modelos.Time timeM = new Persistencia.modelos.Time();
       Persistencia.modelos.Estadio estadioSelecionado = new Persistencia.modelos.Estadio();
       Persistencia.modelos.Partida novaPartida = new Persistencia.modelos.Partida();
       Persistencia.modelos.Campeonato ultimoCampeonato = new Persistencia.modelos.Campeonato();
       PartidaDAO partidaDao = new PartidaDAO();
       CampeonatoDAO campeonatoDao = new CampeonatoDAO();

       String numero = jTextField3.getText();
       String data = jFormattedTextField1.getText();
       String quantidade = jTextField4.getText();
       boolean flagOcorreu = false;
       if(jRadioButton1.isSelected()){flagOcorreu = true;}
        else if(jRadioButton2.isSelected()){flagOcorreu = false;}
       int numero1 = 0;
       int quantidade1=0;
       int idEstadio = 0;
       int idTimeM =0;
       int idTimeV = 0;

        if (!(numero.isEmpty()))
        {
       boolean ehInt =(form.ValidaNumero(jTextField3));
       if(ehInt)  numero1 = Integer.parseInt(numero);
       }
       if (!(quantidade.isEmpty()))
        {
       boolean ehInt =(form.ValidaNumero(jTextField4));
       if(ehInt)   quantidade1 = Integer.parseInt(quantidade);
       }

       if ((numero.isEmpty())||((!jRadioButton1.isSelected()) && (!jRadioButton2.isSelected())) ) {
      //exibe popup de erro
       JOptionPane.showMessageDialog(null, "Atenção aos campos obrigatórios!" ,"Alerta",JOptionPane.WARNING_MESSAGE);
        }
        else if (jComboBox1.getSelectedItem().equals("Selecione uma opção")){
            JOptionPane.showMessageDialog(null, "O campo estadio é obrigatório!" ,"Alerta",JOptionPane.WARNING_MESSAGE);}
        else if (jComboBox4.getSelectedItem().equals("Selecione uma opção") || (jComboBox6.getSelectedItem().equals("Selecione uma opção")) ){
            JOptionPane.showMessageDialog(null, "Os campos de time é obrigatório!" ,"Alerta",JOptionPane.WARNING_MESSAGE);}
        else if((form.validadorData(data)))
        {
                Date dataPartida = null;
            try {
                 dataPartida = form.formataData(data);
            } catch (Exception ex) {
                Logger.getLogger(NovoCampeonato.class.getName()).log(Level.SEVERE, null, ex);
            }
        
        Object timeSelecionadoM = jComboBox4.getSelectedItem();
        String x = timeSelecionadoM.toString();
        int i = x.indexOf(" ");
        String idTime1 = x.substring(0,i);
        idTimeM = Integer.parseInt(idTime1);

        timeM = timeDao.recupera(idTimeM);


        Object timeSelecionadoV = jComboBox6.getSelectedItem();
        String y = timeSelecionadoV.toString();
        int a = y.indexOf(" ");
        String idTime2 = y.substring(0,a);
        idTimeV = Integer.parseInt(idTime2);

        timeV = timeDao.recupera(idTimeV);

        Object estadioSelecionado1 = jComboBox1.getSelectedItem();
        String z = estadioSelecionado1.toString();
        int b = z.indexOf(" ");
        String idEstadio1 = z.substring(0,b);
        idEstadio = Integer.parseInt(idEstadio1);

        estadioSelecionado = estadioDao.recupera(idEstadio);

        int idCampeonato = SeqHelper.getId("campeonato");
        idCampeonato = idCampeonato - 1;
        ultimoCampeonato = campeonatoDao.recupera(idCampeonato);

        novaPartida.setNroRodada(numero1);
        novaPartida.setDataPartida(dataPartida);
        novaPartida.setEstadio(estadioSelecionado);
        novaPartida.setTimeMandante(timeM);
        novaPartida.setTimeVisitante(timeV);
        novaPartida.setQtdTorcedores(quantidade1);
        novaPartida.setFlagOcorreuPartida(flagOcorreu);
        novaPartida.setCampeonato(ultimoCampeonato);

        //incluir objeto no DAO

        partidaDao.salva(novaPartida);

         this.jButton1.setEnabled(false);//botÃ£o inserir fica desabilitado
         // chamar metodo para limpar os campos
        }
       else//se data nÃ£o for vÃ¡lida exibe um popup
        {
         JOptionPane.showMessageDialog(null, " Verifique se o campo de data está válido!" ,"Alerta",JOptionPane.WARNING_MESSAGE);

        }

    }//GEN-LAST:event_jButton1MouseClicked

    private void jComboBox6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox6ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox6ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JComboBox jComboBox6;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration//GEN-END:variables

}
